import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

class Simulateur {
	private Gare gareA;
	private Gare gareB;
	private Gare gareC; 
	private Trajet trajetAB;
	private Trajet trajetBA;
	private Trajet trajetAC;
	private Trajet trajetCA;
	private Trajet trajetBC;
	private Trajet trajetCB;
    Billeterie billeterie = null;
	static final int NB_TRAINS = 5;
	static final int NB_VOYAGEUR = 15;

	public void work() {		
		try {
			System.out.print("Installing security manager...");
			if (System.getSecurityManager() == null) {
				System.setSecurityManager(new SecurityManager());
			}
			System.out.println("done.");

			System.out.print("Getting the registry...");
			Registry registry = LocateRegistry.getRegistry();
			System.out.println("done.");

			System.out.print("Getting the stub...");
			billeterie = (Billeterie) registry.lookup("sncf");
			System.out.println("done.");

			//Instanciation des gares
			gareA = new Gare(1, "gare[A]", billeterie);
			gareB = new Gare(2, "gare[B]", billeterie);
			gareC = new Gare(3, "gare[C]", billeterie);
			//Instanciation des trajets posibles
			trajetAB = new Trajet(gareA, gareB);
			trajetBA = new Trajet(gareB, gareA);
			trajetAC = new Trajet(gareA, gareC);
			trajetCA = new Trajet(gareC, gareA);
			trajetBC = new Trajet(gareB, gareC);
			trajetCB = new Trajet(gareC, gareB);		
			// Instanciation et lancement des trains 
			for (int i = 0; i < NB_TRAINS; i++) {		
				int trajet = (int)( Math.random()*( 6 - 1 + 1 ) ) + 1;
				new Train(i, getTrajet(trajet), billeterie).start();
			}		
			// Instanciation et lancement des voyageurs 
			for (int i = 0; i < NB_VOYAGEUR; i++) {
				int gareAchat = (int)( Math.random()*( 6 - 1 + 1 ) ) + 1;
				new Voyageur(i, getGare(gareAchat)).start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	} // work
	
	public Trajet getTrajet(int numTrajet){
		Trajet trajet = null;
		if (numTrajet == 1){
			trajet = trajetAB;
		} else if (numTrajet == 2){
			trajet = trajetBA;
		} else if (numTrajet == 3){
			trajet = trajetAC;
		} else if (numTrajet == 4){
			trajet = trajetCA;
		} else if (numTrajet == 5){
			trajet = trajetBC;
		} else if (numTrajet == 6){
			trajet = trajetCB;
		} 
		return trajet;
	}
	
	public Gare getGare(int numGare){
		Gare gare = null;
		if (numGare == 1){
			gare = gareA;
		} else if (numGare == 2){
			gare = gareB;
		} else if (numGare == 3){
			gare = gareC;
		} 
		return gare;
	}

	public static void main(String args[]) {
		Simulateur s = new Simulateur();
		s.work();
	}

} // Simulateur